/**
 * @description 持仓分析
 * @author wangjianjun
 */

import React, { PureComponent } from 'react';
import { TableC } from '../../../../../comps';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message, Modal } from 'antd';
import style from './index.less';
const confirm = Modal.confirm;
import { connect } from 'dva';
import 'moment/locale/zh-cn';

@Form.create()
@connect(({ exchangeAgreement, userM: user, loading }) => ({
  exchangeAgreement,
  user,
  loading: loading.effects['exchangeAgreement/getPosition'],
}))
export default class PositionAnalysis extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      columns: [],
      dataSource: [],
      search: null,
    };
  }

  componentDidMount() {}

  UNSAFE_componentWillReceiveProps(nextProps) {
    const init = !this.props.PositionAnalysis ? '' : JSON.stringify(this.props.PositionAnalysis);
    const initN = !nextProps.PositionAnalysis ? '' : JSON.stringify(nextProps.PositionAnalysis);
    if (init != initN) {
      this.setState(
        {
          search: nextProps.PositionAnalysis,
        },
        () => {
          if (nextProps.PositionAnalysis != null) {
            this.getTableData();
          } else {
            this.setState({
              columns: [],
              dataSource: [],
            });
          }
        },
      );
    }
  }

  // 获取持仓数据
  getTableData = () => {
    const { search } = this.state;
    if (search && search.portfolioId && search.exchangeCode) {
      this.props.dispatch({
        type: 'exchangeAgreement/getPosition',
        payload: {
          ...search,
          currentPage: 1,
          numInPage: 500,
        },
        callback: (data) => {
          if (data) {
            this.setState({
              columns: data.columns,
              dataSource: data.dataSource,
            });
            //抛出数据给质押券和标的券
            this.props.getConfigData('selectData', data.dataSource);
          }
        },
      });
    }
  };

  render() {
    const { columns, dataSource } = this.state;
    if (columns && columns.length > 0) {
      columns.map((item, key) => {
        if (item.key == 'pledgeBondCode') {
          item.width = '130px';
          item.fixed = 'left';
        }

        if (item.key == '"pledgeBondName"') {
          item.width = '14%';
        }

        if (item.key == 'canUse') {
          item.width = '14%';
          item.type = 'number';
        }

        if (item.key == 'dueDate') {
          item.width = '14%';
        }

        if (item.key == 'nextPaymentDate') {
          item.width = '14%';
        }

        if (item.key == 'externalRating') {
          item.width = '14%';
        }

        if (item.key == 'remainingDenomination') {
          item.width = '16%';
          item.type = 'money';
        }
      });
    }

    return (
      <div>
        <TableC noDataHeight="115px" columns={this.state.columns} dataSource={this.state.dataSource} loading={this.props.loading} hidePage={true} scroll={{ x: 850, y: 350 }} />
      </div>
    );
  }
}
